using System.Collections;
using UnityEngine;

public class SnowEnabler : MonoBehaviour
{
	public static SnowEnabler instance;

	[SerializeField]
	private GameObject particleObject;

	private bool particleActive;

	[SerializeField]
	private float timeCheck = 60f;

	[SerializeField]
	private float snowProbality = 0.5f;

	private Coroutine checkSnow;

	private void Awake()
	{
		instance = this;
		RoomTrigger.PlayerInRoom += SwitchParticle;
		if (Device.indexWeakDevice != 0 || GameController.instance.curTypeGame == TypeGame.race || GameController.instance.curTypeGame == TypeGame.tanks || GameController.instance.curTypeGame == TypeGame.TDM || GameController.instance.curTypeGame == TypeGame.Zombe)
		{
			snowProbality = 0f;
			base.enabled = false;
			if (particleObject != null)
			{
				particleObject.SetActive(false);
			}
		}
	}

	private void OnEnable()
	{
		StartCheck();
	}

	private void OnDestroy()
	{
		instance = null;
		RoomTrigger.PlayerInRoom -= SwitchParticle;
	}

	private void SwitchParticle(bool inRoom)
	{
		if (inRoom && particleActive)
		{
			Snow(false);
		}
	}

	private IEnumerator CheckSnow()
	{
		while (true)
		{
			bool newSnow = snowProbality > Random.Range(0f, 1f);
			if (particleActive != newSnow)
			{
				Snow(newSnow);
			}
			yield return new WaitForSeconds(timeCheck);
		}
	}

	public void Snow(bool val)
	{
		if (!RoomTrigger.playerInRoom || !val)
		{
			particleActive = val;
			if (particleObject != null)
			{
				particleObject.SetActive(val);
			}
		}
	}

	public void DevSnow(bool value)
	{
		base.enabled = value;
	}

	private void StartCheck()
	{
		if (checkSnow != null)
		{
			StopCoroutine(checkSnow);
			checkSnow = null;
		}
		checkSnow = StartCoroutine(CheckSnow());
	}
}
